package com.block.datapush.special.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.common.exception.NotifyExceptionService;
import com.block.common.service.IApplicationInfoService;
import com.block.common.service.LogFactory;
import com.block.common.vo.ResponseVo;
import com.block.datapush.special.service.ISpecialCostomUserInfoService;
import com.block.db.common.result.CostomUserInfoResult;
import com.block.db.entity.MemberExtInfo;
import com.block.db.entity.SpecialCostomUserInfo;
import com.block.db.mapper.SpecialCostomUserInfoMapper;
import com.block.sms.enums.UseTypeEnum;
import com.block.sms.service.ISmsConfigInfoService;
import com.block.sms.service.ISmsRecordService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2022-09-15
 */
@Service
public class SpecialCostomUserInfoServiceImpl extends ServiceImpl<SpecialCostomUserInfoMapper, SpecialCostomUserInfo> implements ISpecialCostomUserInfoService {

    private static Logger log = LogFactory.getPushLog();

    @Autowired
    ISmsConfigInfoService smsConfigInfoService;
    @Autowired
    IApplicationInfoService applicationInfoService;
    @Autowired
    ISmsRecordService smsRecordService;
    @Autowired
    NotifyExceptionService notifyExceptionService;

    /**
     * 查询机构可推送的子账号信息
     * @param costomInfoId
     * @return
     */
    @Override
    public CostomUserInfoResult findOneCanPushUserByCostomId(Long costomInfoId){
        return this.baseMapper.findOneCanPushUserByCostomId(costomInfoId);
    }


    @Async
    @Override
    public void userInfoNoticePush(CostomUserInfoResult userInfoResult, MemberExtInfo memberExtInfo){

        try {

            UseTypeEnum noticeType = UseTypeEnum.NOTICE;
            String appCode = memberExtInfo.getAppCode();
            if (StrUtil.isEmpty(appCode)){
                appCode = "rxh";
            }

            String mobile  = memberExtInfo.getMobile();
            JSONArray jsonArray = new JSONArray();
            jsonArray.add(StrUtil.format("{}{}", StrUtil.sub(memberExtInfo.getUsername(), 0, 1), ObjectUtil.equal(memberExtInfo.getSex(), 1)?"先生":"女士"));
            jsonArray.add(StrUtil.sub(mobile, mobile.length()-4, mobile.length()));
            ResponseVo responseVo = smsRecordService.sendSms(noticeType, userInfoResult.getAdminPhone(), "1", jsonArray, memberExtInfo.getReqIp(), appCode);

            /**
             * 用户推送记录
             */
            log.info("【短信推送】【机构推送】【uid】{}【adminUid】{}【responseVo】{}", memberExtInfo.getUid(), userInfoResult.getAdminUserId(), JSON.toJSONString(responseVo));

            /**
             * 用户短信推送
             */
            JSONArray jsonArrayUser = new JSONArray();
            jsonArrayUser.add(StrUtil.format("{}{}", StrUtil.sub(memberExtInfo.getUsername(), 0, 1), ObjectUtil.equal(memberExtInfo.getSex(), 1)?"先生":"女士"));
            jsonArrayUser.add(userInfoResult.getAdminPhone());
            ResponseVo responseVo1 = smsRecordService.sendSms(noticeType, memberExtInfo.getMobile(), "2", jsonArrayUser, memberExtInfo.getReqIp(), appCode);

            log.info("【短信推送】【客户推送】【uid】{}【adminUid】{}【smsResultDTO】{}", memberExtInfo.getUid(), userInfoResult.getAdminUserId(), JSON.toJSONString(responseVo1));
        }catch (Exception exception){
            exception.printStackTrace();
            String format = StrUtil.format("【短信推送】【机构推送】【客户推送】异常【uid】{}【adminUid】{}", memberExtInfo.getUid(), userInfoResult.getAdminUserId());
            log.error(format, exception);
            notifyExceptionService.notifyNoticeInfo("pushSmsInfo", "机构推送线索异常", format, exception);
        }
    }

    @Override
    public void updateUserPushTime(Long userInfoId) {
        SpecialCostomUserInfo updateEntity = new SpecialCostomUserInfo();
        updateEntity.setId(userInfoId);
        updateEntity.setLastPushTime(LocalDateTime.now());
        this.updateById(updateEntity);
    }

}
